package com.codeoss.module.stock.service.impl;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.stock.dao.StockMovementMapper;
import com.codeoss.module.stock.model.StockLocator;
import com.codeoss.module.stock.model.StockMovement;
import com.codeoss.module.stock.model.StockMovementConfirm;
import com.codeoss.module.stock.model.StockMovementLine;
import com.codeoss.module.stock.model.StockTransaction;
import com.codeoss.module.stock.service.IStockLocatorService;
import com.codeoss.module.stock.service.IStockMovementConfirmService;
import com.codeoss.module.stock.service.IStockMovementLineService;
import com.codeoss.module.stock.service.IStockMovementService;
import com.codeoss.module.stock.service.IStockStorageService;
import com.codeoss.module.stock.service.IStockTransactionService;

@Service
public class StockMovementService extends BaseService<StockMovementMapper, StockMovement>
		implements IStockMovementService,ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Autowired
	private IStockMovementLineService stockMovementLineService;
    @Autowired
	private IStockStorageService stockStorageService;
	@Autowired
	private IStockTransactionService stockTransactionService;
	@Autowired
	private IStockLocatorService stockLocatorService;
	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<StockMovementLine> stockMovementLineList=stockMovementLineService.getLineListByStockMovementId(id);
		if(stockMovementLineList==null ||stockMovementLineList.size()==0 ) {
			throw new RuntimeException("没有行明细");
		}
		IStockMovementConfirmService stockMovementConfirmService=applicationContext.getBean(IStockMovementConfirmService.class);
		List<StockMovementConfirm> stockMovementConfirmList=stockMovementConfirmService.getConfirmByStockMovementId(id);
		for(StockMovementConfirm stockMovementConfirm:stockMovementConfirmList) {
		    if(!"CO".equals(stockMovementConfirm.getDocStatus())) {
		    	throw new RuntimeException("请先完成调拨确认单"); 
		    }
		}
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		StockMovement stockMovement=getById(id);
		stockMovement.setDocStatus("CO");
		update(stockMovement);
		if(!stockMovement.getApproved()) {
			return;
		}
		List<StockMovementLine> stockMovementLineList=stockMovementLineService.getLineListByStockMovementId(id);
		for(int i=0;i<stockMovementLineList.size();i++) {
			StockMovementLine stockMovementLine=stockMovementLineList.get(i);
			StockLocator stockLocator=stockLocatorService.getById(stockMovementLine.getStockLocatorId());
			StockLocator stockLocatorTo=stockLocatorService.getById(stockMovementLine.getStockLocatorToId());
			
			stockStorageService.add(stockLocator.getStockWarehouseId(), stockLocator.getId(), stockMovementLine.getProductProductId(),
					stockMovementLine.getProductAttrSetInstanceId(), stockMovementLine.getMovementQty().negate(), null, null);
			
			stockStorageService.add(stockLocatorTo.getStockWarehouseId(), stockLocatorTo.getId(), stockMovementLine.getProductProductId(),
					stockMovementLine.getProductAttrSetInstanceId(), stockMovementLine.getMovementQty(), null, null);
			
			StockTransaction stockTransaction=new StockTransaction();
			stockTransaction.setProductProductId(stockMovementLine.getProductProductId());
			stockTransaction.setStockLocatorId(stockLocator.getId());
			stockTransaction.setProductAttrSetInstanceId(stockMovementLine.getProductAttrSetInstanceId());
			stockTransaction.setMovementDate(stockMovement.getMovementDate());
			stockTransaction.setMovementQty(stockMovementLine.getMovementQty().negate());
			stockTransaction.setMovementType("movementout");
			stockTransaction.setStockMovementLineId(stockMovementLine.getId());
			stockTransactionService.insert(stockTransaction);
			
			StockTransaction stockTransactionTo=new StockTransaction();
			stockTransactionTo.setProductProductId(stockMovementLine.getProductProductId());
			stockTransactionTo.setStockLocatorId(stockLocatorTo.getId());
			stockTransactionTo.setProductAttrSetInstanceId(stockMovementLine.getProductAttrSetInstanceId());
			stockTransactionTo.setMovementDate(stockMovement.getMovementDate());
			stockTransactionTo.setMovementQty(stockMovementLine.getMovementQty());
			stockTransactionTo.setMovementType("movementin");
			stockTransactionTo.setStockMovementLineId(stockMovementLine.getId());
			stockTransactionService.insert(stockTransactionTo);
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		StockMovement stockMovement=getById(id);
		stockMovement.setApproved(true);
		update(stockMovement);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		StockMovement stockMovement=getById(id);
		stockMovement.setApproved(false);
		update(stockMovement);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		StockMovement stockMovement=getById(id);
		stockMovement.setDocStatus(docStatus);
		update(stockMovement);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}
}